home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / MakeMake / Source / macros.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-14  |  2.2 KB  |  102 lines  |  [TEXT/MPS ]

  1. # include "defs.h"
  2.  
  3. /*
  4.  * Given a string of the form "name=value", record the name and value
  5.  * in the macro list.  This routine just parses the string;
  6.  * smacro() does the recording.
  7.  */
  8. setmacro (s)
  9. char *s;                /* Input string                */
  10.     {
  11.     char *mname;        /* Pointer to name part        */
  12.     char *mvalue;        /* Pointer to value part    */
  13.     
  14.     /*
  15.      * Find the '='
  16.      */
  17.     mname = s;
  18.     while (*s != EOS && *s != '=')
  19.         s++;
  20.     if (*s == EOS)
  21.         {
  22.         goof ("internal error: \"%s\" not a macro\n",
  23.             mname);
  24.         return;
  25.         }
  26.     /*
  27.      * Replace the '=' with EOS, to null-terminate the name.
  28.      * The value begins in the next char, and is already null-terminated.
  29.      */
  30.     *s = EOS;
  31.     mvalue = s + 1;        /* OK for this to be EOS    */
  32.     
  33.     smacro (mname, mvalue);
  34.     
  35.     *s = '=';            /* Put back the '=', it must be there later. */
  36.     }
  37.  
  38. /*
  39.  * Record a macro
  40.  */
  41. smacro (mname, mvalue)
  42. char *mname;            /* Macro's name        */
  43. char *mvalue;            /* Macro's value    */
  44.     {
  45.     int i;                /* Loop counter        */
  46.     extern char *malloc ();
  47.     
  48.     /*
  49.      * See if the macro is already recorded
  50.      */
  51.     for (i = 0; i < MACROMAX && macrolist[i].mname != NULL; i++)
  52.         {
  53.         if (strcmp (macrolist[i].mname, mname) == 0)
  54.             break;    /* Found it */
  55.         }
  56.     
  57.     if (i == MACROMAX)
  58.         {
  59.         goof ("Too many macros (max %d).\n", MACROMAX);
  60.         return;
  61.         }
  62.     
  63.     /*
  64.      * If the macro was not already recorded,
  65.      * save its name in permanent storage.
  66.      */
  67.     if (macrolist[i].mname == NULL)
  68.         {
  69.         macrolist[i].mname = malloc ((unsigned) strlen (mname) + 1);
  70.         if (macrolist[i].mname == NULL)
  71.             nomemory ();
  72.         strcpy (macrolist[i].mname, mname);
  73.         }
  74.     
  75.     /*
  76.      * If the macro had a previous value,
  77.      * reclaim the storage space and assign the new value.
  78.      */
  79.     if (macrolist[i].mvalue != NULL)
  80.         free (macrolist[i].mvalue);
  81.     macrolist[i].mvalue = malloc ((unsigned) strlen (mvalue) + 1);
  82.     if (macrolist[i].mvalue == NULL)
  83.         nomemory ();
  84.     strcpy (macrolist[i].mvalue, mvalue);
  85.     }
  86.  
  87. /*
  88.  * Look up a macro name and return its value.
  89.  * It is permissible for the value to be an empty string;
  90.  * but if the macro is not recorded at all, return NULL.
  91.  */
  92. char *getmacro (mname)
  93. char *mname;
  94.     {
  95.     int i;
  96.     
  97.     for (i = 0; i < MACROMAX && macrolist[i].mname != NULL; i++)
  98.         if (strcmp (mname, macrolist[i].mname) == 0)
  99.             return (macrolist[i].mvalue);
  100.     return (NULL);
  101.     }
  102.